from django.db import models
from django.db.models import permalink
from django.contrib.auth.models import User
from tagging.fields import TagField
from blog.managers import *
from places.models import Place


class Category(models.Model):
  """ Category """
  title       = models.CharField(maxlength=100)
  slug        = models.SlugField(maxlength=60)
  
  class Meta:
    verbose_name_plural = 'categories'
    db_table = 'blog_categories'
    ordering = ('title',)
  
  def __str__(self):
    return '%s' % self.title
  
  @permalink
  def get_absolute_url(self):
    return ('blog_category_detail', None, { 'slug':self.slug })


class Post(models.Model):
  """ Post model """
  STATUS_CHOICES = (
    (1, 'Draft'),
    (2, 'Live'),
    (3, 'Closed'),
  )
  title       = models.CharField(maxlength=200)
  slug        = models.SlugField(maxlength=60)
  author      = models.ForeignKey(User)
  body        = models.TextField()
  tease       = models.TextField(blank=True)
  status      = models.IntegerField(choices=STATUS_CHOICES, radio_admin=True, default=2)
  publish     = models.DateTimeField()
  created     = models.DateTimeField(auto_now_add=True)
  modified    = models.DateTimeField(auto_now=True)
  categories  = models.ManyToManyField(Category, blank=True)
  place       = models.ForeignKey(Place, blank=True)
  tags        = TagField()
  objects     = ManagerWithPublished()
  live        = LivePostManager()
  
  class Meta:
    db_table  = 'blog_posts'
    ordering  = ('-publish',)
    get_latest_by = 'publish'

  def __str__(self):
    return self.title

  @permalink
  def get_absolute_url(self):
      return ('blog_detail', None, {
        'year'  : self.publish.year,
        'month' : self.publish.strftime('%b').lower(),
        'day'   : self.publish.day,
        'slug'  : self.slug
      })